/*
 * statisticsLibrary.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.library;

import java.io.BufferedReader;
import java.io.IOException;

import java.util.Vector;

import android.util.Log;

import de.studiorutton.access.FileAccess;
import de.studiorutton.access.FileAccessException;
import de.studiorutton.access.FileLocation;
import de.studiorutton.gerengdic.library.LibraryException.ErrorCode;

public class LibraryStatistics {
	private static final String TAG = "learningwords";
	private FileLocation fileLocation;
	protected String language;

	public LibraryStatistics( FileLocation fileLocation, String language ) throws LibraryException {
		this.fileLocation = fileLocation;
		this.language = language;
	}
	
	public Vector<String> getFirstNRecords(int numWords) throws FileAccessException, LibraryException {
		FileAccess fa = new FileAccess(fileLocation);
		BufferedReader bReader = fa.getReader();

		Vector<String> records = new Vector<String>();
		String s;
		int i;
		for (i = 0; i < numWords; i++) {
			try {
				while ((s = bReader.readLine()) != null) {
//					Log.d( TAG, "LibraryStatistics: FirstNRecords: linelength = " + s.length() + "." );
				}
			} catch (IOException e) {
				throw new LibraryException( ErrorCode.ERROR_CANNOT_FIND_NTH_WORD, e.getLocalizedMessage());
			}
			records.add( s );
		}

		return records;
	}
	
	public String getNthRecord( int nthWord ) throws LibraryException, FileAccessException {
		FileAccess fa = new FileAccess( fileLocation );
		BufferedReader bReader = fa.getReader();
		
		String s = null;
		int i = 0;
		while ( i < nthWord ) {
			try {
				if ( (s = bReader.readLine()) == null ) 
					throw new LibraryException( ErrorCode.ERROR_CANNOT_FIND_NTH_WORD, Integer.valueOf(nthWord).toString(), Integer.valueOf(i+1).toString() );
			} catch (IOException e) {
				throw new LibraryException( ErrorCode.ERROR_CANNOT_FIND_NTH_WORD, e.getLocalizedMessage() );
			}
			i++;
		};

		return s;
	}
}
